
 
/* --------------------------------------- Include Files ---------------------------------------- */
//#include "common.h"
#include "stm32f10x.h"
#include "system_timer.h"
#include "driver.h"
#include "uthread.h"


typedef struct
{
	void (*tf)(void);      
	uint8_t tn[8];      
	uint8_t tia;                                         
	uint64_t tep;                                  
}TIT;

/* --------------------------------- Global Function Prototypes --------------------------------- */
void _goes(void);
uint32_t _sct(void (*func)(void), uint8_t *name);
uint32_t _vks(void);
uint64_t _gtk(void);

/* -------------------------------------- Static Variables -------------------------------------- */
static uint32_t ttc = 0;                    
static uint32_t tec = 0;          
static TIT til[15] = {{0},{0},{0},{0}}; 
static uint64_t ot = 0;

// 启动任务管理
void _goes()
{
    while(1){
		    for(tec=0; tec<15; tec++){      
					  if(til[tec].tia == 1){
						    til[tec].tf();	
						}
				}
			ot++;
		}
}

// 创建任务
uint32_t _sct(void (*_tfc)(), uint8_t *_tn)
{	
	uint32_t i = 0 ;
	uint32_t k = 0;
	
	if(ttc == 15){
		return 0x00;
	}

	for(i=0; i<15; i++){
	    if(til[i].tia==0){
			    til[i].tf = _tfc;				           
	        while((*_tn != 0x00)&&(k<(8-1)))		
	        {
					    if(k < 8){
				          til[i].tn[k++] = *(_tn++);
			        }
	        }
	        til[i].tn[k++] = 0X00;
					til[i].tia = 1;                            
          ttc++;                                                        
					return (i+1);                                             
			}
	}
  return 0x00;
}

// 结束任务
uint32_t _vks()
{
	til[tec].tia = 0; 
	ttc--;
	return 0x01;
}

// 获取时钟
uint64_t _gtk()
{
    return ot;
}
